#!/bin/bash

#
# remove different echo command behaviour on different OS
#
if test "`echo -e xxx`" = "xxx"
then
    echo="echo -e"
else
    echo=echo
fi

if [ $# = 0 ]
then
	$echo "\nusage:  runtest ds_system\n"
	$echo "ds_system can be redhate5_64, ubuntu1404, python or \"jenkins <dir>\""
	exit 1
fi

#
# test host
#

REDHATE5_64_HOST=esrflinux2-1a
PYTHON_HOST=controls01
UBUNTU1404_HOST=pcrogucki

HOST=`hostname`
SERV_NAME=devTest


case $1 in
redhate5_64 )
	if [ $HOST != $REDHATE5_64_HOST ]
	then
		$echo "For redhate5_64, test should run on "$REDHATE5_64_HOST
		exit 1
	fi
	DIR=bin/redhate5_64
	;;
#
ubuntu1404 )
	if [ $HOST != $UBUNTU1404_HOST ]
	then
		$echo "For ubuntu 14.04, test should run on "$UBUTU1204_HOST
		exit 1
	fi
	DIR=bin
	DEV1="dev/test/10"
	DEV2="dev/test/11"
	DEV3="dev/test/12"
	DEV1_ALIAS="et_alias"
	ATTR_ALIAS="et_attr_alias"
	INST_NAME=api
	DEV_MCAST="no/mcast/test"
	FWD_DEV="dev/fwdtest/10"
	OS=Linux
	;;
#
python )
	if [ $HOST != $PYTHON_HOST ]
	then
		$echo "For python, test should run on "$PYTHON_HOST
		exit 1
	fi
	DIR=suse93
	;;
#
jenkins )
	if [ $# != 2 ]
	then
		echo "Missing base directory"
		exit -1
	fi
	BASE_DIR=$2
	for i in $NODE_LABELS
	do
		echo $i

		if [ $i = "ubuntu12.04" ]
		then
			echo "Ubuntu 12.04 OS"
			DIR="$BASE_DIR/ubuntu1204/bin"
			DEV1="test/ubuntu1204/10"
			DEV2="test/ubuntu1204/11"
			DEV3="test/ubuntu1204/12"
			FWD_DEV="test/fwd_ubuntu1204/10"
			DEV_MCAST="test/centos5/30"
			DEV1_ALIAS="ubuntu1204_alias"
			ATTR_ALIAS="ubuntu1204_attr_alias"
			INST_NAME=ubuntu1204
			OS=Linux
		fi

		if [ $i = "Windows_VC9" ]
		then
			echo "Windows VC9"
			DIR="$BASE_DIR/win32/bin"
			DEV1="test/win32_vc9/10"
			DEV2="test/win32_vc9/11"
			DEV3="test/win32_vc9/12"
			FWD_DEV="test/fwd_win32_vc9/10"
#			DEV_MCAST="test/win64_vc10/30"
			DEV_MCAST="no/mcast/test"
			DEV1_ALIAS="win32_vc9_alias"
			ATTR_ALIAS="win32_vc9_attr_alias"
			INST_NAME=win32_vc9
			OS=Windows
		fi

		if [ $i = "Windows_VC10" ]
		then
			echo "Windows VC10 (64 bits)"
			DIR="$BASE_DIR/win64/bin"
			DEV1="test/win64_vc10/10"
			DEV2="test/win64_vc10/11"
			DEV3="test/win64_vc10/12"
			FWD_DEV="test/fwd_win64_vc10/10"
#			DEV_MCAST="test/win32_vc9/30"
			DEV_MCAST="no/mcast/test"
			DEV1_ALIAS="win64_vc10_alias"
			ATTR_ALIAS="win64_vc10_attr_alias"
			INST_NAME=win64_vc10
			OS=Windows
		fi
	
		if [ $i = "centos5" ]
		then
			echo "CentOS 5 OS"
			DIR="$BASE_DIR/centos5/bin"
			DEV1="test/centos5/10"
			DEV2="test/centos5/11"
			DEV3="test/centos5/12"
			FWD_DEV="test/fwd_centos5/10"
			DEV_MCAST="test/ubuntu1204/30"
			DEV1_ALIAS="centos5_alias"
			ATTR_ALIAS="centos5_attr_alias"
			INST_NAME=centos5
			OS=Linux
		fi

		if [ $i = "redhate4" ]
		then
			echo "Red Hat 4 OS"
			DIR="$BASE_DIR/redhate4/bin"
			DEV1="test/redhate4/10"
			DEV2="test/redhate4/11"
			DEV3="test/redhate4/12"
			FWD_DEV="test/fwd_redhate4/10"
			DEV_MCAST="test/debian6/30"
			DEV1_ALIAS="redhate4_alias"
			ATTR_ALIAS="redhate4_attr_alias"
			INST_NAME=redhate4
			OS=Linux
		fi

		if [ $i = "redhate5" ]
		then
			echo "Red Hat 5 OS"
			DIR="$BASE_DIR/redhate5/bin"
			DEV1="test/redhate5/10"
			DEV2="test/redhate5/11"
			DEV3="test/redhate5/12"
			FWD_DEV="test/fwd_redhate5/10"
			DEV_MCAST="no/mcast/test"
			DEV1_ALIAS="redhate5_alias"
			ATTR_ALIAS="redhate5_attr_alias"
			INST_NAME=redhate5
			OS=Linux
		fi

		if [ $i = "debian6" ]
		then
			echo "Debian 6 OS"
			DIR="$BASE_DIR/debian6/bin"
			DEV1="test/debian6/10"
			DEV2="test/debian6/11"
			DEV3="test/debian6/12"
			FWD_DEV="test/fwd_debian6/10"
			DEV_MCAST="test/redhate4/30"
			DEV1_ALIAS="debian6_alias"
			ATTR_ALIAS="debian6_attr_alias"
			INST_NAME=debian6
			OS=Linux
		fi

		if [ $i = "debian7_tango" ]
		then
			echo "Debian 7 OS"
			DIR="$BASE_DIR/debian7/bin"
			DEV1="test/debian7/10"
			DEV2="test/debian7/11"
			DEV3="test/debian7/12"
			FWD_DEV="test/fwd_debian7/10"
			DEV_MCAST="test/redhate4/30"
			DEV1_ALIAS="debian7_alias"
			ATTR_ALIAS="debian7_attr_alias"
			INST_NAME=debian7
			OS=Linux
		fi

		if [ $i = "debian8_tango" ]
		then
			echo "Debian 8 OS"
			DIR="$BASE_DIR/debian8/bin"
			DEV1="test/debian8/10"
			DEV2="test/debian8/11"
			DEV3="test/debian8/12"
			FWD_DEV="test/fwd_debian8/10"
			DEV_MCAST="test/redhate4/30"
			DEV1_ALIAS="debian8_alias"
			ATTR_ALIAS="debian8_attr_alias"
			INST_NAME=debian8
			OS=Linux
		fi

	done
	;;
#
* )
	$echo "\nunknown system"
	exit 1
	;;
esac

check_return_value () {
if [ $1 != "0" ]
then
	echo "Test Suite FAILED !!!!!!!!!!!!!!!!!!"
	echo "Leaving test suite, try to fix it"
	date
	sleep 120
	date
	$DIR/StopPoll $DEV1 --kill $DEV_MCAST
	exit -1
fi
}

#
# First, print used libs
#
if [ $OS != "Windows" ]
then
	ldd $DIR/misc_devproxy
fi

#
# Due to different host CPU power and in case of test suites compilation,
# the DS used for mcast test could be started at very different time ccording to host
# Wait for mcast test device to be ready before starting test
#
# Our Windows VC9 host with Samba is so slow.....
#

if [ $OS != "Windows" ]
then
	LOOP=10
else
	LOOP=35
fi

#if [ $DEV_MCAST != "no/mcast/test" ]
#then
#	$DIR/wait_mcast_dev $DEV_MCAST $LOOP
#	if [ $? != "0" ]
#	then
#		echo "It seems that the remote DS to test multicast feature is not started"
#		echo "Exit test suite !!!!!!!!!!!!!!!!!!"
#		date
#		exit -1
#	fi
#fi

#
# Wait of a random value before starting test (Spread database load)
#

val=$RANDOM
let "val=$val % 30"
let val=$val+5
echo "Random sleeping of $val seconds"
sleep $val
ps -ef | grep devTest
ps -ef | grep FwdTest
n=`date`
echo "Starting test at $n"

#
# Run the CXX test suite
#

$echo "Running the new Test Suite"
CURR_DIR="`pwd`"

#
# Don't know why but when running this script from Jenkins on windows,
# the HOST string has 9 characters instead of 8. There is a \013 (carriage return)
# appended at the end
#

if [ $OS = "Windows" ]
then
	if [ $INST_NAME = "win32_vc9" ]
	then
		FULL_HOSTNAME=`host PCTAUREL | awk '{print $1}'`
		touch win32/vc9/test_suite/runner/runner.cpp
	else
		FULL_HOSTNAME=`host W-CB126-1 | awk '{print $1}'`
		touch win64/vc10/test_suite/runner/runner.cpp
	fi
	FULL_HOSTNAME=`echo $FULL_HOSTNAME | tr "[:upper:]" "[:lower:]"`
else
	FULL_HOSTNAME=`host $HOST | awk '{print $1}'`
fi

echo "FULL_HOSTNAME="$FULL_HOSTNAME

if [ $OS = "Windows" ]
then
	#WIN_OUT_DIR="c:\\jenkins\\workspace\\tango_test_suite\\operatingsystems\\windows_vc9\\cpp_test_suite\\win32\\vc9\\test_suite\\runner\\../../../../new_tests/out\\"
	WIN_OUT_DIR="`cygpath -w -p $CURR_DIR`"
	RUN_CMD="$DIR/runner --device1=$DEV1 --device2=$DEV2 --device3=$DEV3 --fwd_device=$FWD_DEV --loop=5 --fulldsname=$SERV_NAME/$INST_NAME --serverhost=$FULL_HOSTNAME --clienthost=$FULL_HOSTNAME --serverversion=5 --docurl=http://www.tango-controls.org --devtype=TestDevice --dbserver=sys/database/2 --outpath=/tmp/ --refpath=$WIN_OUT_DIR\\new_tests\\out\\ --loglevel=0 --dsloglevel=3 --suiteloop=3 --devicealias=$DEV1_ALIAS --attributealias=$ATTR_ALIAS --local_device=$DEV1 --remote_device=$DEV_MCAST"

	if [ $INST_NAME = "win32_vc9" ]
	then
		sleep 10
	fi
	
	echo "runner command="$RUN_CMD
	$RUN_CMD
else
	export RUN_CMD="runner --device1=$DEV1 --device2=$DEV2 --device3=$DEV3 --fwd_device=$FWD_DEV --loop=1 --fulldsname=$SERV_NAME/$INST_NAME --serverhost=$FULL_HOSTNAME  --clienthost=$FULL_HOSTNAME --serverversion=5 --docurl=http://www.tango-controls.org --devtype=TestDevice --dbserver=sys/database/2 --outpath=/tmp/ --refpath=$CURR_DIR/new_tests/out/ --loglevel=0 --dsloglevel=3 --suiteloop=1 --devicealias=$DEV1_ALIAS --attributealias=$ATTR_ALIAS"

	if [ $INST_NAME != "redhate5" ]
	then
		RUN_CMD="$RUN_CMD --local_device=$DEV1 --remote_device=$DEV_MCAST"
	fi
	
	# create runner_run script
	RUNNER_RUN=$DIR/runner_run
	if [ -f $RUNNER_RUN ]
	then
		rm -f $RUNNER_RUN
	fi
	echo "#!/bin/bash" > $RUNNER_RUN
	echo "./$RUN_CMD" >> $RUNNER_RUN
	
	# run test suite
	echo "runner command="$RUN_CMD
	rm /tmp/orb.trace
	export ORBtraceLevel=40
	export ORBtraceTime=1
	export ORBtraceFile=/tmp/orb.trace
	eval $DIR/$RUN_CMD
fi
ret=$?
check_return_value $ret

#
# Kill the remote device server used for mcast test
#

$DIR/StopPoll --kill $DEV_MCAST

unset ORBtraceLevel
unset ORBtraceFile
unset ORBtraceTime

#
# Run test suite
#

$echo "Testing miscellaneous DeviceProxy features"
$DIR/misc_devproxy $DEV1 $SERV_NAME/$INST_NAME
ret=$?
check_return_value $ret
#
$echo "\nTesting insertion and extraction into DeviceData objects for all types"
$DIR/cmd_types $DEV1 10
ret=$?
check_return_value $ret
#
$echo "\nTesting extraction from DeviceAttribute objects for all types"
$DIR/attr_types $DEV1 10
ret=$?
check_return_value $ret
#
$echo "\nTesting attribute configuration changes"
$DIR/att_conf $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting miscellaneous DeviceData object features"
$DIR/misc_devdata
ret=$?
check_return_value $ret
#
$echo "\nTesting miscellaneous DeviceAttribute object features"
$DIR/misc_devattr
ret=$?
check_return_value $ret
#
$echo "\nTesting writing attribute (with the eight data types) using DeviceAttribute"
$DIR/write_attr $DEV1 10
ret=$?
check_return_value $ret
#
$echo "\nTesting copy constructor and assignement operator of the DeviceProxy class"
$DIR/copy_devproxy $DEV1 $DEV2 $DEV3
ret=$?
check_return_value $ret
#
$echo "\nTesting miscellaneous attribute features"
$DIR/attr_misc $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting object property methods of the Database class"
$DIR/obj_prop
ret=$?
check_return_value $ret
#
$echo "\nTesting command_history and attribute_history DeviceProxy methods"
$DIR/Poll $DEV1
ret=$?
check_return_value $ret
sleep 7
$DIR/read_hist $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting polling of new attribute data types"
$DIR/poll_attr $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting all DeviceProxy polling methods"
$DIR/poll_met $DEV1 $DEV2 $DEV1_ALIAS
ret=$?
check_return_value $ret
#
$echo "\nTesting polling threads pool"
$DIR/poll_pool $DEV1 $DEV2 $INST_NAME
ret=$?
check_return_value $ret
#
$DIR/StopPoll $DEV1
#
$echo "\nTesting all AttributeProxy methods"
$DIR/attr_proxy $DEV1/Short_attr_rw
ret=$?
check_return_value $ret
#
$echo "\nTesting writing spectrum and image attributes"
$DIR/write_attr_3 $DEV1 10
ret=$?
check_return_value $ret
#
$echo "\nTesting polled command with external triggering"
$DIR/read_hist_ext $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting individual polling buffer depth and polling methods in DeviceImpl"
$DIR/ring_depth $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting memorized attributes"
$DIR/mem_att $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting state and status read as attributes"
$DIR/state_attr $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting RDS alarm"
$DIR/rds $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting Device Server Cache"
$DIR/ds_cache
ret=$?
check_return_value $ret
#
$echo "\nTesting Write_Read attribute"
$DIR/w_r_attr $DEV1
ret=$?
check_return_value $ret
#
if [ $1 = "jenkins" ]
then
	PATH=$PATH:$DIR
else
	PATH=$PATH:./$DIR
fi
$echo "\nTesting device locking feature"
$DIR/lock $DEV1 $DEV2
ret=$?
check_return_value $ret

#
$echo "\nTesting the sub device diagnostics"
$DIR/sub_dev $DEV1 $DEV2 $DEV3
ret=$?
check_return_value $ret
#
$echo "\nTesting printing operators"
$DIR/print_data $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting printing attribute property"
$DIR/attr_manip $DEV1
ret=$?
check_return_value $ret
#
if [ $INST_NAME = "redhate4" ]
then
	$echo "\nCheck user classes size"
	$DIR/size
	ret=$?
	check_return_value $ret
fi
#
$echo "\nTesting resetting attribute event conf. reset (need event system...)"
$DIR/ConfEventBugClient $DEV3
ret=$?
check_return_value $ret
#
$echo "\nTesting resetting attribute configuration (to user default, to class default, to lib default...)"
$DIR/attr_conf_test $DEV1
ret=$?
check_return_value $ret
#
#	Now, test asynchronous features
#
cd asyn;./runtest $*
#
#
#	Now, test events
#
cd ../event;./runtest $*


